package com.example.testapplication.ui.main;

import android.app.Application;

import androidx.annotation.NonNull;

import com.example.testapplication.event.Event;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.bus.RxBus;
import me.goldze.mvvmhabit.bus.RxSubscriptions;
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;

public class MainViewModel extends BaseViewModel {
    private Disposable mSubscription;
    public MainViewModel(@NonNull Application application) {
        super(application);
    }

    //封装一个界面发生改变的观察者
    public UIChangeObservable uc = new UIChangeObservable();



    public class UIChangeObservable {
        //Rxbus观察者
        public SingleLiveEvent<Boolean> pRxBusEvent = new SingleLiveEvent<>();
    }




    //注册RxBus
    @Override
    public void registerRxBus() {
        super.registerRxBus();
        mSubscription = RxBus.getDefault().toObservable(Event.class)
                .subscribe(new Consumer<Event>() {
                    @Override
                    public void accept(Event s) throws Exception {
                        uc.pRxBusEvent.setValue(s.bool);
                    }
                });
        //将订阅者加入管理站
        RxSubscriptions.add(mSubscription);
    }

    //移除RxBus
    @Override
    public void removeRxBus() {
        super.removeRxBus();
        //将订阅者从管理站中移除
        RxSubscriptions.remove(mSubscription);
    }
}
